# 创建并写入文件
with open('example.txt', 'w') as file:
    file.write("This is an example of file I/O in Python.\n")
    file.write("We are learning file reading and writing operations.\n")
    print("File created and written successfully.")

# 读取文件内容
with open('example.txt', 'r') as file:
    content = file.read()
    print("\nFile content:")
    print(content)

# 追加内容到文件
with open('example.txt', 'a') as file:
    file.write("This line is added later.\n")
    print("\nAppended new line to the file.")

# 读取文件所有行并逐行处理
with open('example.txt', 'r') as file:
    lines = file.readlines()
    print("\nReading file line by line:")
    for line in lines:
        print(line.strip())  # strip() 去掉末尾的换行符
